
using Admin2505.Application.Services;
using Microsoft.AspNetCore.Mvc;

namespace Admin2505.Api.Controllers;

[ApiController]
[Route("api/[controller]")]

public class AuthController : ControllerBase

{
    private readonly  AuthService _authController;

    public AuthController( AuthService authController)
    {
         _authController = authController;
    }


    [HttpPost("register")]
    public async Task<IActionResult>  Register([FromBody] RegisterRequest  request)
    {
        try
        {
            var user = await _authController.RegisterAsync(request.Username, request.Password);
            return Ok(new{user.Username});
        }
        catch (Exception ex)
        {
            return BadRequest(new{ex.Message});
        }
    }

    [HttpPost("login")]
    public async Task<IActionResult> Login([FromBody] LoginRequest request)
    {
        
        
            var user = await _authController.LoginAsync(request.Username, request.Password);
            if (user == null)
                return Unauthorized("用户名或密码错误");
            return Ok(user);
       
    }

    public class RegisterRequest
    {
        public required string Username { get; set; }
        public required string Password { get; set; }
    }

    public class LoginRequest
    {
        public required string Username { get; set; }
        public required string Password { get; set; }
    }
}